const { querySql, user, good, business, good_evaluate, shp_profit, user_apply_cash, user_dividend, user_meet, user_note_invite, user_question, op } = require("./baseModel");
class SqlUpdate {
  async init() {
    // await this.udateGoodSelePrice()
    // await this.udateShippingFee()
    // await this.initDb()
  }

  // 更新邮费
  async udateShippingFee() {
    let resBusiness = await querySql(`select a.business_id,sum(a.shipping_fee) as shipping_fee_total  from business_profit_dtl as a
                                  where a.shipping_fee!=0
                                  GROUP BY a.business_id`);

    if (resBusiness && resBusiness.length > 0) {
      for (let i = 0; i < resBusiness.length; i++) {
        let row = resBusiness[i];
        let { shipping_fee_total, business_id } = row;
        await business.update({ shipping_fee_total: shipping_fee_total }, { where: { id: business_id } });
      }
    }
  }

  async udateGoodSelePrice() {
    let goodRows = await good.findAll();

    for (let i = 0; i < goodRows.length; i++) {
      let goodRow = goodRows[i];
      await goodRow.update({ sale_price: goodRow.specificationStr.sale_price });
    }
  }
  // 测试数据库专用用于 初始化db
  async initDb() {
    await order.destroy({ where: {}, truncate: true });
    await order_dtls.destroy({ where: {}, truncate: true });
    await good_evaluate.destroy({ where: {}, truncate: true });
    await shp_profit.destroy({ where: {}, truncate: true });
    await user_apply_cash.destroy({ where: {}, truncate: true });
    await user_dividend.destroy({ where: {}, truncate: true });
    await user_meet.destroy({ where: {}, truncate: true });
    await user_note_invite.destroy({ where: {}, truncate: true });
    await user_question.destroy({ where: {}, truncate: true });
    let userRows = await user.findAll({});
    for (let i = 0; i < userRows.length; i++) {
      let userRow = userRows[i];
      await userRow.update({ in_come: 0, extend_come: 0, total_consume: 0, overage_money: 0, consume_equity: 0, seed_number: 10, vip_grade_id: 1 });
    }
  }
}

const sqlUpdate = new SqlUpdate();
module.exports = sqlUpdate;
